//
//  FileManager.m
//  Router
//
//  Created by teki on 15/10/22.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "FileManager.h"

@implementation FileManager


+ (NSString *)getDocumentsPath{
    //获取Documents路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSLog(@"Documents path:%@", path);
    return path;
}


+ (NSString *)getLibraryPath
{
    //获取Library路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSLog(@"Library path:%@", path);
    return path;
}

+ (NSString *)getCachePath
{
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath objectAtIndex:0];
    NSLog(@"app_home_lib_cache: %@",cachePath);
    return cachePath;
}

+ (NSString *)getTemporaryPath
{
    NSString *paths = NSTemporaryDirectory();
    NSLog(@"temporary Path:%@",paths);
    return paths;
}

+ (BOOL)createDirectoryWithDirectoryName:(NSString *)dircetoryName
{
    NSString *documentsPath =[FileManager getDocumentsPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *iOSDirectory = [documentsPath stringByAppendingPathComponent:dircetoryName];
    BOOL isSuccess = [fileManager createDirectoryAtPath:iOSDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if (isSuccess) {
        NSLog(@"success");
    } else {
        NSLog(@"fail");
    }
    return isSuccess;
}


+ (NSString *)directoryPathWithDirectoryName:(NSString *)dircetoryName{
    NSString *documentsPath =[FileManager getDocumentsPath];
    NSString *iOSDirectory = [documentsPath stringByAppendingPathComponent:dircetoryName];
    return iOSDirectory;
}


+ (BOOL)createFileWithFileName:(NSString *)fileName
{
    NSString *documentsPath =[FileManager getDocumentsPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *iOSPath = [documentsPath stringByAppendingPathComponent:fileName];
    BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil];
    if (isSuccess) {
        NSLog(@"success");
    } else {
        NSLog(@"fail");
    }
    return isSuccess;
}


+ (NSString *)filePathWithFileName:(NSString *)fileName
{
    NSString *documentsPath =[self getDocumentsPath];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
    return filePath;
}


+ (BOOL)isExistAtPath:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isExist = [manager fileExistsAtPath:filePath];
    return isExist;
}


+ (unsigned long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:filePath];
    if (isExist){
        unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
        return fileSize;
    } else {
        NSLog(@"file is not exist");
        return 0;
    }
}

+ (unsigned long long)folderSizeAtPath:(NSString*)folderPath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:folderPath];
    if (isExist){
        NSEnumerator *childFileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];
        unsigned long long folderSize = 0;
        NSString *fileName = @"";
        while ((fileName = [childFileEnumerator nextObject]) != nil){
            NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        return folderSize / (1024.0 * 1024.0);
    } else {
        NSLog(@"file is not exist");
        return 0;
    }
}


+ (BOOL)deleteFileWithFilePath:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isSuccess = [fileManager removeItemAtPath:filePath error:nil];
    if (isSuccess) {
        NSLog(@"delete success");
    }else{
        NSLog(@"delete fail");
    }
    return isSuccess;
}

+ (NSArray *)getFilelistFromDocument
{
    
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [arr objectAtIndex:0];
    
    NSFileManager* fm=[NSFileManager defaultManager];
    if(![fm fileExistsAtPath:path]){
        [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    //取得一个目录下得所有文件名
    
    NSArray *files = [fm subpathsAtPath:path];
    return files;
}


+ (void)removeDocumentWithFileNamePrefix:(NSString *)fileNamePrefix
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSArray *fileNames = [self getFilelistFromDocument];
    for (NSString *fileName in fileNames) {
        NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];
        if ([fileManager fileExistsAtPath:filePath] && [fileName hasPrefix:fileNamePrefix]) {
            [fileManager removeItemAtPath:filePath error:nil];
        }
        
    }
}




@end
